package com.hpsk.enums;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ReadConverterContext;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 * @author huangwang2832511019
 */

@Getter
@AllArgsConstructor
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
public enum DeleteFlagEnum implements BaseEnum{
    DELETE("DELETE", "删除"),
    NORMAL("NOT_DELETE", "正常");

    @EnumValue
    private final String name;
    private final String label;
    private static DeleteFlagEnum setDeleteFlagEnum(String name){
        DeleteFlagEnum[] values = values();
        for (DeleteFlagEnum value : values) {
            if (value.getName().equals(name)) {
                return value;
            }
        }
        return null;
    }
    @Override
    public String getCode() {
        return this.name();
    }

    public static class ExcelConverter implements Converter<Object>{
        @Override
        public WriteCellData<?> convertToExcelData(WriteConverterContext<Object> context) throws Exception {
            String label1 = DeleteFlagEnum.setDeleteFlagEnum(context.getValue().toString()).getLabel();
            context.setValue(label1);
            return Converter.super.convertToExcelData(context);
        }

        /**
         * xlsx中java对象转cell数据方法
         *
         * @param value               枚举对象
         * @param contentProperty     contentProperty
         * @param globalConfiguration globalConfiguration
         * @return 写入到cell中的数据
         */
        @Override
        public WriteCellData<?> convertToExcelData(Object value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
            return new WriteCellData<>(value.toString());
        }

        @Override
        public DeleteFlagEnum convertToJavaData(ReadConverterContext<?> context) throws Exception {
            return DeleteFlagEnum.setDeleteFlagEnum(Converter.super.convertToJavaData(context).toString());
        }
        /**
         * xlsx中cell转java对象方法
         *
         * @param cellData            cell中的数据
         * @param contentProperty     contentProperty
         * @param globalConfiguration globalConfiguration
         * @return 转换后的枚举值
         */
        @Override
        public DeleteFlagEnum convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
            return BaseEnum.valueOf(cellData.getStringValue(), DeleteFlagEnum.class);
        }

        /**
         * 标明Excel中的类型
         *
         * @return 标明Excel中的类型
         */
        @Override
        public CellDataTypeEnum supportExcelTypeKey() {
            return CellDataTypeEnum.STRING;
        }

        /**
         * 标明数据库中的类型
         *
         * @return 标明数据库中的类型
         */
        @Override
        public Class<?> supportJavaTypeKey() {
            return String.class;
        }
    }
}
